package huixue.cn.com.rxapp.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import huixue.cn.com.library.app.BaseApplication;
import huixue.cn.com.library.constants.Constant;
import huixue.cn.com.library.util.LogUtils;
import huixue.cn.com.library.util.SPUtil;
import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.base.BaseFragment;
import huixue.cn.com.rxapp.entity.Channel;


public class HomeFragment extends BaseFragment {

    private List<Channel> mSelectedChannels = new ArrayList<>();
    private List<Channel> mUnSelectedChannels = new ArrayList<>();
    private String[] mChannelCodes;
    private Gson mGson = new Gson();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }


    @Override
    protected void initViewsAndEvents() {
        initChannelData();
        initChannelFragments();
    }

    private void initChannelData() {
        String selectedChannelJson = (String)SPUtil.get(BaseApplication.getApplictionContext(),Constant.SELECTED_CHANNEL_JSON, "");
        String unselectChannel = (String)SPUtil.get(BaseApplication.getApplictionContext(),Constant.UNSELECTED_CHANNEL_JSON, "");

        if (TextUtils.isEmpty(selectedChannelJson) || TextUtils.isEmpty(unselectChannel)) {
            //本地没有title
            String[] channels = getResources().getStringArray(R.array.channel);
            String[] channelCodes = getResources().getStringArray(R.array.channel_code);
            //默认添加了全部频道
            for (int i = 0; i < channelCodes.length; i++) {
                String title = channels[i];
                String code = channelCodes[i];
                mSelectedChannels.add(new Channel(title, code));
            }

            selectedChannelJson = mGson.toJson(mSelectedChannels);//将集合转换成json字符串
            LogUtils.i("selectedChannelJson:" + selectedChannelJson);
            SPUtil.put(getContext(),Constant.SELECTED_CHANNEL_JSON, selectedChannelJson);//保存到sp
        } else {
            //之前添加过
            List<Channel> selectedChannel = mGson.fromJson(selectedChannelJson, new TypeToken<List<Channel>>() {
            }.getType());
            List<Channel> unselectedChannel = mGson.fromJson(unselectChannel, new TypeToken<List<Channel>>() {
            }.getType());
            mSelectedChannels.addAll(selectedChannel);
            mUnSelectedChannels.addAll(unselectedChannel);
        }
    }

    private void initChannelFragments() {

    }
}
